#include <sys/shm.h>  // shared memory
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#include "common.h"
#include "psql_operation.h"

// int get_user_shm_id_by_name(char* username,char* shm)
// {
//     if(username==NULL) return 0;
//     printf("\nusername is :%s",username);
//     struct Connect_Info* p_con_info=(struct Connect_Info*)shm;
//     int ret_user_shm_id=0;
//     if(p_con_info->next_connect_info!=NULL)
//     {
//         p_con_info=p_con_info->next_connect_info;
//         while (p_con_info->location!=0)
//         {            
//             if(strcmp(p_con_info->username,username)==0)
//             {
//                 ret_user_shm_id=p_con_info->user_shm_id;
//                 return ret_user_shm_id;
//             }
//             printf("\nget_user_shm_id:%s ",p_con_info->user_shm_id);
        
//             p_con_info=p_con_info->next_connect_info;
            
//         }
//     }
    
//     return ret_user_shm_id;   
// }

int get_user_shm_id_by_name(char* username)
{
    int user_id=get_id_by_username(username);
    int user_shm_id=0;
    if(user_id!=0)    user_shm_id=get_user_shm_id(1024,user_id);
    return user_shm_id;

}

int get_lenght_of_connect_info_list(struct Connect_Info* connect_info)
{
    if(connect_info==NULL) return 0;
    int i=0;
    while(connect_info!=NULL)
    {
        connect_info=connect_info->next_connect_info;
        i++;
    }
    return i;
}

int del_connect_info(struct Connect_Info* connect_info,char* shm)
{
    if(connect_info==NULL) return 0;
    struct Connect_Info* p_con_info=(struct Connect_Info*)shm;
    int i=1;
    while (p_con_info->next_connect_info!=NULL)
    {
        if(p_con_info->next_connect_info->connectfd==connect_info->connectfd)
        {
            struct Connect_Info* tmp_con_info=p_con_info->next_connect_info;
            p_con_info->next_connect_info=p_con_info->next_connect_info->next_connect_info;
            memset(tmp_con_info,0,sizeof(struct Connect_Info));
            return i;
        }
        p_con_info=p_con_info->next_connect_info;
        i++;
        
    }
    return i;
}

int add_connect_info(struct Connect_Info* connect_info, char* shm)
{
    if(connect_info==NULL) return 0;
    
    printf("%p\n",shm);
    
    
    struct Connect_Info* last_shm=(struct Connect_Info*)shm;

    if(last_shm->location!=0){
        while(last_shm->next_connect_info!=NULL)
        {
            last_shm=last_shm->next_connect_info;
            printf("%p\n",last_shm);
        }
    }
    
    int i=1;

    struct Connect_Info* first_empty_shm=(struct Connect_Info*)shm;
    first_empty_shm++;
    while (first_empty_shm->location!=0)
    {
        first_empty_shm++;
        i++;
        printf("%p\n",first_empty_shm);
    }
    connect_info->location=i;
    memcpy(first_empty_shm,connect_info,sizeof(struct Connect_Info));
    if(last_shm!=first_empty_shm) last_shm->next_connect_info=first_empty_shm;
    return i;
}

bool is_online(char* username,struct Connect_Info* connect_info)
{
    if(connect_info==NULL) return false;
    while(connect_info!=NULL)
    {
        if(strcmp(username,connect_info->username)==0 ) return true;
        connect_info=connect_info->next_connect_info;
    }
    return true;
}

int get_shm_id(int size){
    key_t key;
    int shmid;
     // 获取key值
     if((key = ftok("/", 2000)) == -1)
     {
         perror("ftok error");
         exit(1);
     }
    // 创建共享内存
    if((shmid = shmget(key, size, IPC_CREAT|0666)) == -1)
    {
        perror("Create Shared Memory Error");
        exit(1);
    }
    return shmid;
}


int get_user_shm_id(int size,int user_id){
    key_t key;
    int shmid;
     // 获取key值
     if((key = ftok("/", user_id)) == -1)
     {
         perror("ftok error");
         exit(1);
     }
    // 创建共享内存
    if((shmid = shmget(key, size, IPC_CREAT|0666)) == -1)
    {
        perror("Create Shared Memory Error");
        exit(1);
    }
    return shmid;
}

// char* connect_shm(int shmid){
//     // 连接共享内存
//     char* shm = (char*)shmat(shmid, NULL, 0);
//     if((int)shm == -1)
//     {
//         perror("Attach Shared Memory Error");
//         exit(1);
//     }
//     printf("%d",shm);
//     return shm;
// }

